﻿// /////////////////////////////////////////////////////////////////////
// 
//  Codeplex JSAnalyse
//  Copyright (c) 2011
//  JSAnalyse.Core   MemberExpressionInfo.cs
// 
// /////////////////////////////////////////////////////////////////////

using System.Collections.Generic;
using JSAnalyse.Core;
using JSAnalyse.Test.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSAnalyse.Test
{
    /// <summary>
    /// Test class for the prototype syntax and dependencies
    /// </summary>
    /// <remarks></remarks>
    [TestClass]
    public class PrototypeSyntaxTest
    {
        /// <summary>
        /// A simple dependency check test
        /// </summary>
        [TestMethod]
        public void PrototypeSyntaxSimpleTest()
        {
            var mainJs = "Main.js";
            var ref1Js = "Ref1.js";
            var ref2Js = "Ref2.js";

            var allowedDependencies = new List<AllowedDependencyTest>();

            var fileInfoMain = new JavascriptFileInfo(mainJs,
                                                      @"String.format = function () {
                                                            var s = arguments[0];
                                                            for (var i = 0; i < arguments.length - 1; i++) {
                                                                var reg = new RegExp(""\\{"" + i + ""\\}"", ""gm"");
                                                                s = s.replace(reg, arguments[i + 1]);
                                                            }
                                                            return s;
                                                        };");
            var fileInfoRef1 = new JavascriptFileInfo(ref1Js,
                                                      @"function function2() {
                                                            var id = '1234'
                                                            return String.format('Id Property: {0}', id);
                                                        }");
            var fileInfoRef2 = new JavascriptFileInfo(ref2Js,
                                                      @"function function2() {
                                                            return String.testMethod();
                                                        }");

            var dependencyRunner = new DependencyRunner(
                new List<JavascriptFileInfo> { fileInfoMain, fileInfoRef1, fileInfoRef2 },
                new AllowedDependenciesTest(allowedDependencies)
                );

            var result = dependencyRunner.Start();
            var mainResult = GeneralHelper.GetResultItem(result, mainJs);
            var ref1Result = GeneralHelper.GetResultItem(result, ref1Js);
            var ref2Result = GeneralHelper.GetResultItem(result, ref2Js);
            Assert.AreEqual(3, result.Count);

            Assert.IsNotNull(mainResult);
            Assert.AreEqual(0, mainResult.WrongFileDependencies.Count);

            Assert.IsNotNull(ref1Result);
            Assert.AreEqual(1, ref1Result.WrongFileDependencies.Count);

            Assert.IsNotNull(ref2Result);
            Assert.AreEqual(0, ref2Result.WrongFileDependencies.Count);
        }
    }
}
